﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sbook.Data;
using Sbook.Business;
using System.Data;
using System.Data.SqlClient;
namespace Sbook.Shopping
{
    public class UserCart : Data.SqlDataProvider
    {
        public string PName { get; set; }
        public string PImg { get; set; }
        public string Quantity { get; set; }
        public string Price { get; set; }
        public string UId { get; set; }
        public string DeliveryPrice { get; set; }

        public UserCart IDataReaderUserCart(IDataReader dr)
        {
            UserCart obj = new UserCart();
            obj.PName = (dr["PName"] is DBNull) ? string.Empty : dr["PName"].ToString();
            obj.PImg = (dr["PImg"] is DBNull) ? string.Empty : dr["PImg"].ToString();
            obj.Quantity = (dr["Quantity"] is DBNull) ? string.Empty : dr["Quantity"].ToString();
            obj.Price = (dr["Price"] is DBNull) ? string.Empty : dr["Price"].ToString();
            obj.UId = (dr["UId"] is DBNull) ? string.Empty : dr["UId"].ToString();
            obj.DeliveryPrice = (dr["DeliveryPrice"] is DBNull) ? string.Empty : dr["DeliveryPrice"].ToString();
            return obj;
        }

        public static List<UserCart> GetUserCart(string UId)
        {
            List<UserCart> listCart = new List<UserCart>();
            Data.SqlDataProvider db = new SqlDataProvider();
            using (SqlCommand cmd = new SqlCommand("sp_SoppingCart", GetConnection()))
            {
                UserCart obj = new UserCart();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@UId", UId));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        listCart.Add(obj.IDataReaderUserCart(dr));
                    }
                }
            }
            return listCart;
        }
    }
}